package cn.abcsys.devops.application.service;/**
 * Copyright (2017, ) Institute of Software, Chinese Academy of Sciences
 * Copyright (2017, ) Bocloud Co,. Lmt
 */

import cn.abcsys.devops.application.model.ParamsApplication;
import cn.abcsys.devops.application.restful.DeployerRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author xuyuanjia2017@otcaix.iscsa.ac.cn
 * @date 2017/6/20 0020
 * say sth.
 */
@Service("applicationService")
public class ApplicationService {

    @Resource(name="deployerRequest")
    private DeployerRequest dr;
    //http://IP:9003/devops-deployer/create.do?instanceCore.instanceNamespace=default&
    // instanceCore.instanceName=tomcat&instanceCore.ip=40.125.203.202&instanceCore.instanceType=kubernetes&
    // instanceCore.instanceImage=registry.cn-hangzhou.aliyuncs.com/xuyuanjia/docker:tomcat-centos&instanceCore.instanceCpu=1&
    // instanceCore.instanceMemory=1Gi&instanceCore.instanceNetwork=bridge&instanceCore.instanceImagePullSecret=my-secret
    public Map<String,Object> compactDeployerCreateRcParams(ParamsApplication pa){
        Map<String,Object> resMap = new HashMap<>();
        System.out.println(pa.applicationCore.getApplicationCoreUsername());
        resMap.put("instanceCore.instanceNamespace",pa.applicationCore.getApplicationCoreUsername());
        System.out.println(pa.applicationCore.getApplicationCoreName());
        resMap.put("instanceCore.instanceName",pa.applicationCore.getApplicationCoreName());
        System.out.println(pa.getIp());
        resMap.put("instanceCore.ip",pa.getIp());
        System.out.println(pa.applicationCore.getApplicationCoreType());
        resMap.put("instanceCore.instanceType",pa.applicationCore.getApplicationCoreType());
        System.out.println(pa.instanceImage.getConfigImageName()+":"+((pa.instanceImage.getConfigImageTag()==null ||pa.instanceImage.getConfigImageTag().equals("") )?"latest":pa.instanceImage.getConfigImageTag()));
        resMap.put("instanceCore.instanceImage",pa.instanceImage.getConfigImageName()+":"+((pa.instanceImage.getConfigImageTag()==null ||pa.instanceImage.getConfigImageTag().equals("") )?"latest":pa.instanceImage.getConfigImageTag()));
        System.out.println(pa.configBasic.getConfigBasicCpu());
        resMap.put("instanceCore.instanceCpu",pa.configBasic.getConfigBasicCpu());
        System.out.println(pa.configBasic.getConfigBasicMemory());
        resMap.put("instanceCore.instanceMemory",pa.configBasic.getConfigBasicMemory()+"Mi");
        System.out.println(pa.configBasic.getConfigBasicNetwork());
        resMap.put("instanceCore.instanceNetwork",pa.configBasic.getConfigBasicNetwork());
        System.out.println(pa.instanceImage.getConfigImageSecret());
        resMap.put("instanceCore.instanceImagePullSecret",pa.instanceImage.getConfigImageSecret());
        System.out.println(pa.applicationCore.getApplicationCoreRename());
        resMap.put("instanceCore.appName",pa.applicationCore.getApplicationCoreRename());
        System.out.println(pa.instanceImage.getConfigImageCommands());
        resMap.put("instanceCore.cmd",pa.instanceImage.getConfigImageCommands());//configImageCommands
        System.out.println(pa.configEnvsListString);
        resMap.put("instanceEnvsListString",pa.configEnvsListString);
        System.out.println(pa.configVolumesListString);
        resMap.put("instanceVolumesListString",pa.configVolumesListString);
        System.out.println(pa.applicationCore.getApplicationRuntimeId());
        resMap.put("instanceCore.applicationRuntimeId",pa.applicationCore.getApplicationRuntimeId());
        System.out.println(pa.applicationCore.getApplicationCoreId());
        resMap.put("instanceCore.appId",pa.applicationCore.getApplicationCoreId());
        System.out.println(pa.configPortsListString);
        resMap.put("instancePortsString",pa.configPortsListString);

        return resMap;
    }

    public Map<String,Object> getDeployerCreateRc(ParamsApplication pa){
        Map<String,Object> params = compactDeployerCreateRcParams(pa);
        return dr.getCreateRc(params);
    }

    public Map<String,Object> compactDeployerDeleteRcParams(ParamsApplication pa){
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("instanceCore.instanceCoreId",pa.air.getInstanceCoreId());
        return resMap;
    }

    public Map<String,Object> getDeployerDeleteRc(ParamsApplication pa){
        Map<String,Object> params = compactDeployerDeleteRcParams(pa);
        return dr.getDeleteRc(params);
    }

}
